<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <button onclick="subscriber()">订阅者</button>
    <button onclick="two()">订阅者2</button>
    <br>
    <button onclick="sendMsg()">发布者</button>
</body>
<script>
    //创建公众平台
    let bus = {
        //订阅者模式
        list: [],
        //发布者
        emit(msg) {
            console.log(msg);
            this.list.forEach(item => {
                console.log(item);
                item(msg)
            })
        },
        //订阅者
        on(callback) {
            console.log('订阅成功');
            this.list.push(callback)
        }
    }
    // 订阅消息
    function subscriber() {
        //参数为一个回调函数
        bus.on((msg) => {
            console.log('收到消息拉', msg);
        });
    }
    function two() {
        bus.on((msg) => {
            console.log('我也收到了', msg);
        })
    }
    // 发布消息
    function sendMsg() {
        bus.emit('好好学习天天向上2')
    }
</script>

</html>